package org.stvd.listener.workflow;

import org.activiti.engine.delegate.event.ActivitiEntityEvent;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.stvd.common.security.SecurityUserHolder;
import org.stvd.core.util.DateUtil;
import org.stvd.entities.workflow.WfIndex;
import org.stvd.service.workflow.WfIndexService;

/**
 * 流程实例开始执行
 */
public class ProcessStartedListener implements EventHandler{

    @Autowired
    WfIndexService wfIndexService;
    
    public void handle(ActivitiEvent event) {
        
        System.out.println("Event received: " + event.getType());

        ActivitiEntityEvent entityEvent = (ActivitiEntityEvent) event;
        ProcessInstance processInstance = (ProcessInstance) entityEvent.getEntity();
        
        WfIndex wfIndex = new WfIndex();
        wfIndex.setStartTime(DateUtil.getSystemDate());
        wfIndex.setProcDefId(processInstance.getProcessDefinitionId());
        wfIndex.setProcDefKey(processInstance.getProcessDefinitionKey());
        wfIndex.setProcInstId(processInstance.getId());
        wfIndex.setCandidate("");
        wfIndex.setGroupId("");
        wfIndex.setAssignee("");
        wfIndexService.insert(wfIndex);
    }  
    
}